/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include "atom.h"
#include "syslog.h"
#include "debug.h"
#include <string.h>
#include "numberical.h"

#ifdef COM_USE_SYSLOG

#ifdef COM_USE_DBG_COLOR
#define _DBG_LOG_HDR(lvl_name, color_n)                    \
    KPrint("\033["#color_n"m[" lvl_name "] ")
#else
#define _DBG_LOG_HDR(lvl_name, color_n)                    \
    KPrint("[" lvl_name "] ")
#endif /* DBG_COLOR */

static int _mask_level = COM_SYSLOG_MASK_LEVEL;

/*
 * The color for terminal (foreground)
 * BLACK    30
 * RED      31
 * GREEN    32
 * YELLOW   33
 * BLUE     34
 * PURPLE   35
 * CYAN     36
 * WHITE    37
 */
void Syslog( int priority, const char *format, ... )
{
    int level = (priority & LOG_PRIMASK);

    if( level <= _mask_level ){
        switch(level)                                       
        {    
            case LOG_EMERG:   _DBG_LOG_HDR("S", 35); break;     
            case LOG_ALERT:   _DBG_LOG_HDR("A", 36); break;   
            case LOG_CRIT:    _DBG_LOG_HDR("C", 31); break;                                             
            case LOG_ERR:     _DBG_LOG_HDR("E", 31); break; 
            case LOG_WARNING: _DBG_LOG_HDR("W", 33); break; 
            case LOG_INFO:    _DBG_LOG_HDR("I", 32); break; 
            case LOG_DEBUG:   _DBG_LOG_HDR("D", 0); break;  
            default: break;                                 
        }                                                   
        va_list ap;
        va_start(ap, format);
    #ifdef COM_USE_MYPRINTF 
        char buf[COM_MYPRINTF_BUFFER] = {0};
        _vsnprintf(buf, COM_MYPRINTF_BUFFER, format, ap);
        KPrint(buf);
        KPrint("\r\n");
    #else
        printf(format, ap);
        KPrint("\r\n");
    #endif
        va_end(ap);
        DBG_COLOR(0);                                      
    }
}

void SyslogSetMask( int mask )
{
    _mask_level = mask;
}

#endif